package mobi.hami.huapin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import mobi.hami.huapin.dao.HuaPinMapper;
import mobi.hami.huapin.dao.ProductMapper;
import mobi.hami.huapin.daobase.CrudMapper;
import mobi.hami.huapin.model.HuaPin;
import mobi.hami.huapin.service.HuaPinService;
import mobi.hami.huapin.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Create by 小峰 on 2018/8/22.
 */
@Service
public class HuaPinServiceImpl implements HuaPinService{

    @Autowired
    private HuaPinMapper huaPinMapper;

    @Override
    public int save(HuaPin huaPin) {
        huaPinMapper.insertSelective(huaPin);
        return  huaPin.getId();
    }

    @Override
    public int update(HuaPin huaPin) {
        return huaPinMapper.updateByPrimaryKeySelective(huaPin);
    }

    @Override
    public HuaPin selectOnbyId(Integer id) {
        return huaPinMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<HuaPin> selectListByAddressId(Integer id) {
        HuaPin huaPin= new HuaPin();
        huaPin.setAddress_id(id);
        huaPin.setStatus(1);
        huaPin.setType(2);
        return huaPinMapper.select(huaPin);
    }

    /**
     * 获取当天拼团正在进行的信息
     * @return
     */
    public List<HuaPin> getCurrentDayHuaPinList(){
        return  huaPinMapper.getCurrentDayHuaPinList();
    }


    /**
     * 根据地址,获取当前可以再拼的拼团信息
     * @param map
     * @return
     */
    public Map<String,Object> getHuaPinByAddressId(Map<String, Object> map){
        Integer addressId=0;
        Integer pageIndex = 1;
        Integer pageSize = 10;
        if (null != map && !map.isEmpty()) {
            addressId = map.get("addressId") == null ? 0 : Integer.parseInt(map.get("addressId").toString());
            pageIndex = map.get("pageIndex") == null ? 1 : Integer.parseInt(map.get("pageIndex").toString());
            pageIndex = pageIndex == 0 ? 1 : pageIndex;
            pageSize = map.get("pageSize") == null ? 10 : Integer.parseInt(map.get("pageSize").toString());
            pageSize = pageSize == 0 ? 10 : pageSize;
        }
        Page page = PageHelper.startPage(pageIndex, pageSize, true).reasonable(true);
        List<Map> dataList=huaPinMapper.getHuaPinByAddressId(addressId);
        return BeanUtil.ResultResponseMap(page, dataList, pageSize, pageIndex);
    }


}
